package com.leo.foodmaster.commons.entity;

import com.leo.foodmaster.commons.enums.MsgEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

/**
 * <p>
 * 同意结果返回类
 * </p>
 *
 * @author ：Leo
 * @since ：2021-06-18 11:46
 */
@Data
@ApiModel(value = "响应信息主体")
@ToString
@NoArgsConstructor
public class Result<T> {

    @Getter
    @Setter
    @ApiModelProperty(value = "返回标记：成功标记=200，失败标记非200")
    private Integer code;

    @Getter
    @Setter
    @ApiModelProperty(value = "返回信息")
    private String msg;

    @Getter
    @Setter
    @ApiModelProperty(value = "数据")
    private T data;

    private static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public static <T> Result<T> success() {
        return restResult(null, MsgEnum.SERVICE_SUCCESS.getCode(), MsgEnum.SERVICE_SUCCESS.getMsg());
    }

    public static <T> Result<T> success(T data) {
        return restResult(data, MsgEnum.SERVICE_SUCCESS.getCode(), MsgEnum.SERVICE_SUCCESS.getMsg());
    }

    public static <T> Result<T> success(T data, String msg) {
        return restResult(data, MsgEnum.SERVICE_SUCCESS.getCode(), msg);
    }
    public static <T> Result<T> success(MsgEnum code) {
        return restResult(null, code.getCode(), code.getMsg());
    }
    public static <T> Result<T> fail() {
        return restResult(null, MsgEnum.SERVICE_ERROR.getCode(), MsgEnum.SERVICE_ERROR.getMsg());
    }

    public static <T> Result<T> fail(String msg) {
        return restResult(null, MsgEnum.SERVICE_ERROR.getCode(), msg);
    }

    public static <T> Result<T> fail(MsgEnum code) {
        return restResult(null, code.getCode(), code.getMsg());
    }

    public static <T> Result<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

}